using Front._1117_2;
using MySqlConnector;

namespace WindowsFormsQuickStart._1127;

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using var connection =
            new MySqlConnection(
                "server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        String sql = "SELECT * FROM admin_info where admin_account=@account and admin_password=@pwd;";

        using var command = new MySqlCommand(sql, connection);
        //获取账号密码
        String account = this.textBox1.Text;
        String password = this.textBox2.Text;
        //对密码进行md5加密处理

        string pwd = MD5Helper.GetMD5(password);
        //给sql传参
        command.Parameters.AddWithValue("account", account);
        command.Parameters.AddWithValue("pwd", pwd);

        using var reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            //登录成功, 跳转到主窗体
            MainForm mainForm = new MainForm();
            mainForm.Show();
            //隐藏当前窗体，打开主窗体
            this.Hide();
        }
        else
        {
            //登录失败，使用messagebox.show显示账号或密码错误，同时清空两个文本框的数据
            MessageBox.Show("账号或密码错误！");
            this.textBox1.Text = "";
            this.textBox2.Text = "";
        }
        while (reader.Read())
            Console.WriteLine(reader.GetInt32(0));
    }

    private void Login_Load_1(object sender, EventArgs e)
    {
        this.textBox1.Text = "admin_super01";
        this.textBox2.Text = "123456";
    }
}